package com.aurora.oa.message.presenter;


import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.message.bean.MessageTypeBean;
import com.aurora.oa.message.bean.MessageCountVo;
import com.aurora.oa.message.view.MessageFragmentView;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2017/03/14
*/

public class MessageFragmentPresenterImpl implements MessageFragmentPresenter{

    MessageFragmentView messageView;
    RequestHttpModel requestHttpModel;
    public MessageFragmentPresenterImpl(MessageFragmentView messageView) {
        this.messageView = messageView;
        requestHttpModel = new RequestHttpModelImpl();
    }

    @Override
    public void requestMessageTypeData() {
        String url = AuroraConstants.GET_MESSAGE_COUNT_URL;
        requestHttpModel.requestHttpDataString(url, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                Gson gson = new Gson();
                Type type =new TypeToken<MessageCountVo>(){}.getType();
                MessageCountVo result  =gson.fromJson(body,type);
                messageView.refreshMessagePage(getMessageTypeBeanList(result));
                messageView.showMyTotalCreatMessage(result.getPopcreate());

            }

            @Override
            public void onRequestDataFail(String message) {

            }
        });
    }

    @Override
    public void showBaseInfo() {
        messageView.showBaseInfo();
    }

    /**
     * 解析json的数据变成需要展示的数据结构
     * @param messageCountVo
     */
    private List<MessageTypeBean> getMessageTypeBeanList(MessageCountVo messageCountVo) {
        List<MessageTypeBean> messageTypeList = new ArrayList<>();
        messageTypeList.add(new MessageTypeBean(AuroraConstants.Message.GROUP_MESSAGE_TYPE,messageCountVo.getCompanytot(),messageCountVo.getCompanyunread()));
        messageTypeList.add(new MessageTypeBean(AuroraConstants.Message.DEPART_MESSAGE_TYPE,messageCountVo.getDeptot(),messageCountVo.getDepunread()));
        messageTypeList.add(new MessageTypeBean(AuroraConstants.Message.PERSON_MESSAGE_TYPE,messageCountVo.getPoptot(),messageCountVo.getPopunread()));
        return  messageTypeList;
    }
}